package cn.com.qingz.core.aop;

import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import cn.com.qingz.core.listener.ApplicationInitListener;
import cn.com.qingz.core.model.UpdateUserInfo;

/**
 * @author jiulong.hu
 * 自动植入 updateInfo 无需每次硬编码.
 */
//@Aspect
//@Component
public class UpdateInfoHelper {

	private Logger log = Logger.getLogger(ApplicationInitListener.class);
	
	/**
	 * 切入点
	 */ 
	@Pointcut("execution(* cn.com.qingz.modules..service..*.do*(..)) && !execution(* cn.com.qingz.modules.system.service.impl.BasicServiceImpl.doInitApplication(..))")
	public void joinUpdateInfo() {

	}
	
	//对于 dosave 命名开头的方法 的入参  植入创建人 ,创建时间   do* 方法开头的植入最后更新人 最后更新时间.
	@Before("joinUpdateInfo()")
	public void doBeforeJoinUpdateInfo(JoinPoint joinPoint) throws Exception {

//		String targetName = joinPoint.getTarget().getClass().getName();
//		log.info("targetName:" + targetName);
		String methodName = joinPoint.getSignature().getName();
//		log.info("methodName:" + methodName);
		Object[] arguments = joinPoint.getArgs();
		
		String currentUser = SecurityUtils.getSubject().getPrincipal().toString();
		
		for (Object object : arguments) {
			
			if(object instanceof UpdateUserInfo ){
				UpdateUserInfo currentEntity = (UpdateUserInfo) object;
				if(methodName.startsWith("doSave")){
					currentEntity.setCreatetime(new Date());
					currentEntity.setCreateuser(currentUser);
				}
				currentEntity.setLmodifytime(new Date()); 
				currentEntity.setLmodifyuser(currentUser);
			}
		}
	}

}
